#include <iostream>
#include <string>
#include <vector>
#include <cstdlib>
#include <fstream>
#include <ctime>
#include "quest.h"
#include "game.h"
#include "zsc.h"

using namespace std;


class Menu{
public:
    void run();
private:
    void menuWrite();
    void newGame();
        string setClass();
        string setName();
    void loadGame();
        void playGame(Game &G);
    void Fill(vector<Quest> &v);

};


void Menu::Fill(vector<Quest> &qvec) {
    ifstream f;
    f.open("plot2.txt");
    int cap;
    f>>cap;
    int i=0;
   //m vector<Quest> qvec;
    qvec.resize(cap/*+1*/);

//    f >> t;
    //qvec[0].Set("A jatek prologusa.");
    bool l=false;
    while(i<cap/*+1*/ || !l) {
        string pr="";
        f>>pr;
        if ( pr == "E") {
            pr="";
            int elso;         f >> elso;
            int masodik;      f >> masodik;
            qvec[i-1].setjunc(true,elso,masodik);
        }
        else if (pr == "f") {
            pr="";
            int smegoldas;    f >>  smegoldas;
            int skozelites;   f >>  skozelites;
            int sXP;          f >>  sXP;
            int sunalomLvl;   f >> sunalomLvl;
            string str="";
            std::string sfeladat= ": ";
            while (str!="*") {
                f >> str;
                sfeladat+= " " + str;
            }
            qvec[i-1].setQ(smegoldas,skozelites,sXP,sunalomLvl,sfeladat);
        }
        else if (pr== "h") {
            string name="";
            f >> name;
            int sunalomLvl;
            f >> sunalomLvl;

            string str="";
            std::string sstory = "* ";
            while (str!="*") {
                f >> str;
                sstory+= " " + str;
            }

            int sHP;
            f >>  sHP;
            int sAtk;
            f >>  sAtk;
            int sXP;
            f >>  sXP;
            qvec[i-1].setF(name,sunalomLvl,sstory,sHP,sAtk,sXP);
            if (i==cap) l=true;
        } else {
            string str="";
            string text= "* " + pr;
            while (str!="*") {
                f >> str;
                text+= " " + str;
            }
            pr="";
      //  cout << "alma" << i << " *** ";

            qvec[i].Set(text);
            ++i;
         }
    }

   // qvec[0].putText();
   /* for (int i=0;i<qvec.size();++i) {
            qvec[i].putText();
          //  if (qvec[i].elag) cout << "---" << i+1 << ". ea: " << qvec[i].elagegy << " " << qvec[i].elagketto << endl;
    }*/
    f.close();
}
void Menu::run()
{
    try {
    char c='0';
    string str;
    do {
        menuWrite();
        cin >> c;

        switch (c) {
            case '1': newGame(); break;
            case '2': loadGame(); break;
        }
        std::cin.clear();
        getline(std::cin,str);
    } while(c!='0' && c!='1' && c!='2');
    } catch (Game::error e)
    {
        cout << "Nincs ilyen mentes!";
    }
}

void Menu::menuWrite()
{
    cout << endl << endl;
    cout << " 1. - Uj jatek" << endl;
    cout << " 2. - Jatek betoltese" << endl;
    cout << " 0. - Kilepes" << endl;
}


void Menu::newGame(){
    cout << "- Uj jatek -";

    vector<Quest> quests;

    Fill(quests);


    string cl = setClass();
    string name=setName();

    Game G(quests,cl,name);

    G.startJourney();
    playGame(G);

}

void Menu::loadGame(){
    vector<Quest> quests;
    Fill(quests);
    cout << "Adja meg a mentes nevet (a karakter neve): " << endl;

    ifstream in;
    string fajl;
    cin >> fajl;
    string fajl2 = fajl + ".txt";
    in.open(fajl2.c_str());
    if (in.fail())
        {
            in.clear();
             throw Game::NO_SUCH_SAVE;
        }
    string kaszt; in >> kaszt;
    int lvl; in >> lvl;
    int atk; in >> atk;
    int def; in >> def;
    int xp; in >> xp;
    int boredom; in >> boredom;
    int abnum; in >> abnum;
    std::vector<string> ab;
    for(int i=0;i<abnum;++i)
    {
        string abl;
        in >> abl;
        ab.push_back(abl);
    }
    int qdb; in >> qdb;
    in.close();
  //  Game G(quests,kaszt,fajl);
    Game G(quests,kaszt,lvl,atk,def,xp,boredom,abnum,ab,qdb,fajl);

    playGame(G);
}

string Menu::setClass(){
    int cl;
    string scl;
    string str;
    do {
        cout << "- Kaszt valasztasa -" << endl;
        cout << "1. - Harcos" << endl;
        cout << "2. - Varazslo" << endl;
        cout << "3. - Tolvaj" << endl;
        cin >> cl;
        switch (cl)
        {
            case 1 : scl = "Harcos"; break;
            case 2 : scl = "Varazslo"; break;
            case 3 : scl = "Tolvaj"; break;
        }
        std::cin.clear();
        getline(std::cin,str);
    } while (cl!=1 && cl!=2 && cl!=3);
    return scl;
}

string Menu::setName(){
    string name;
    cout << "- Karakternev -" << endl;
    cout << "A Karakter neve:" << endl;
    cin >> name;
    return name;

}

void Menu::playGame(Game &G){
    char exit;
    string str;
    do
    {
        G.nextQuest();
       // exit=' ';
        cout << "(Folytatas(*) Mentes(s) Kilepes(q)" << endl;
        cin >> exit;
        if (exit == 'q') exit=true;
        if (exit == 's') G.save();
        std::cin.clear();
        getline(std::cin,str);

        if (G.getDQ()!=G.getQNumber()) std::cout << "Gratulalunk teljesitetted a jatekot!";
    } while (exit!=true && G.getDQ()!=G.getQNumber());
}

int main()
{
    std::srand(std::time(0));
    Menu a;
    a.run();
    return 0;
}
